package com.enation.app.javashop.core.goods.service;

import com.enation.app.javashop.core.base.CachePrefix;
import com.enation.app.javashop.core.goods.model.dos.GoodsDO;
import com.enation.app.javashop.core.goods.model.dos.GoodsSkuDO;
import com.enation.app.javashop.core.goods.model.vo.GoodsQuantityVO;
import com.enation.app.javashop.framework.cache.Cache;
import com.enation.app.javashop.framework.database.DaoSupport;
import com.enation.app.javashop.framework.test.BaseTest;
import org.junit.Ignore;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 库存测试数据初始器
 * @author kingapex
 * @version 1.0
 * @since 7.0.0
 * 2018/6/25
 */
@Ignore
public class QuantityDataInitiator extends BaseTest {



    @Autowired
    protected DaoSupport goodsDaoSupport;


    /**
     * 模拟的商品列表
     */
    protected   List<GoodsDO> goodsList ;

    /**
     * 模拟的sku列表
     */
    protected    List<GoodsSkuDO> skuList;


    @Autowired
    private Cache cache;


    /**
     * 构建测试的商品数据
     * 共三个商品，每个商品的名字为“quantityTest-”+1，2，3
     * 每个商品两个sku
     * 商品及suk的为存都为0
     * @return
     */
    protected void init(){

        //要扣减的库存
        List<GoodsQuantityVO> goodsQuantityList = new ArrayList<>();
        goodsList = new ArrayList<>();
        skuList = new ArrayList<>();

        //构建三个商品
        for (int i=0;i<3 ;i++){
            String goodsName  = "quantityTest-"+i;

            GoodsDO goods = new GoodsDO();
            goods.setGoodsName(goodsName);

            goods.setEnableQuantity(0);

            goods.setQuantity(0);

            goodsDaoSupport.insert(goods);
            Integer goodsId = goodsDaoSupport.getLastId("es_goods");

            goods.setGoodsId(goodsId);
            goodsList.add(goods);


            GoodsSkuDO sku1 = new GoodsSkuDO();
            sku1.setGoodsName(goodsName);

            sku1.setQuantity(0);
            sku1.setGoodsId(goodsId);
            sku1.setEnableQuantity(0);


            GoodsSkuDO sku2 = new GoodsSkuDO();
            sku2.setGoodsName(goodsName);
            sku2.setQuantity(0);
            sku2.setGoodsId(goodsId);
            sku2.setEnableQuantity(0);


            goodsDaoSupport.insert(sku1);
            Integer sku1Id = goodsDaoSupport.getLastId("es_goods_sku");
            sku1.setSkuId(sku1Id);

            goodsDaoSupport.insert(sku2);
            Integer sku2Id = goodsDaoSupport.getLastId("es_goods_sku");
            sku2.setSkuId(sku2Id);

            skuList.add(sku1);
            skuList.add(sku2);

            cache.remove (CachePrefix.GOODS.getPrefix() + goodsId);
            cache.remove ( CachePrefix.SKU.getPrefix() + sku1Id);
            cache.remove ( CachePrefix.SKU.getPrefix() + sku2Id);
        }

    }


    /**
     * 清除测试数据
     */
    protected void clearTestData() {
        for (GoodsDO goods : goodsList) {
            goodsDaoSupport.execute("delete from es_goods where goods_id=?",goods.getGoodsId());
            goodsDaoSupport.execute("delete from es_goods_sku where goods_id=?",goods.getGoodsId());
        }

        this.goodsDaoSupport.execute("TRUNCATE es_quantity_log");

    }
    /**
     * 从sku列表中找到某个商品的sku
     * @param goodsId 商品id
     * @return
     */
    protected List<GoodsSkuDO> findGoodsSku(int goodsId) {
        List<GoodsSkuDO> result = new ArrayList<>();

        for (GoodsSkuDO sku : skuList) {
            if (sku.getGoodsId() == goodsId) {
                result.add(sku);
            }

        }

        return result;
    }

}
